package chapter

import scala.collection.mutable.ArrayBuffer

object Chapter9 extends App {

  /*  val html = io.Source.fromURL("http://www.baidu.com", "UTF-8").mkString

  val srcPattern = """(?is)<\s*img[^>]*src\s*=\s*['"\s]*([^'"]+)['"\s]*[^>]*>""".r

  for (srcPattern(s) <- srcPattern findAllIn html) println(s)

  println(io.Source.stdin.getLines)

  val a1 = Array(1, 2, 3, 4)
  val a2 = Array(1, 2, 3, 4)
  println(a1 == a2)
  println(a1 equals a2)

  println(a1.corresponds(a2)(_ == _))*/

  sealed trait Stream[+A]
  case object Empty extends Stream[Nothing]
  case class Cons[+A](h: () => A, t: () => Stream[A]) extends Stream[A]

  case class LikeData(cid: String, status: Int, ver: Long, seq: Int) extends Ordered[LikeData] {
    override def compare(that: LikeData): Int = (ver - that.ver).toInt
  }

  val clientData = Map(("1", LikeData("1", 0, 0, 0)), ("2", LikeData("2", 1, 1, 1)), ("3", LikeData("3", 0, 0, 3)), ("4", LikeData("4", 0, 0, 4)))
  val serverData = Map(("1", LikeData("1", 0, 0, 0)), ("2", LikeData("2", 1, 0, 0)), ("3", LikeData("3", 0, 0, 3)), ("5", LikeData("5", 0, 0, 5))) //Map.empty[String, LikeData]

  /*  (2,LikeData(2,1,1,0))
(1,LikeData(1,0,1,0))
(3,LikeData(3,0,2,3))*/

  //  val finalData = clientData.toList ++ serverData
  //submit a new interface syncLike
  def mergeLikeData2(clientData: Map[String, LikeData], serverData: Map[String, LikeData]) = {
    implicit def map2List(map: Map[String, LikeData]) = map.toList
    val sameData = clientData intersect serverData
    val diffClient = clientData diff sameData
    val diffServer = serverData diff sameData
    println(s"sameData:$sameData\ndiffClient:$diffClient\ndiffServer:$diffServer")

    val diffData = (diffClient ++ diffServer).groupBy(_._1).map(_._2.max)
    val unUpdateData = diffData intersect diffServer
    val syncDate = diffData diff unUpdateData
    (syncDate.toMap, unUpdateData.toMap)
  }

  val a = ArrayBuffer(1, 3, 2, 4, 2, 35, 32, 3, 4, 2)
  a.sorted

  println("sssssssss" + (clientData.toList intersect serverData.toList))

  def mergeLikeData(clientData: Map[String, LikeData], serverData: Map[String, LikeData]) = {
    implicit def map2List(map: Map[String, LikeData]) = map.toList
    val finalData = clientData ++ serverData
    //    println("ȥ��ǰ" + finalData + "\n" + "ȥ�غ�" + finalData.distinct)
    finalData.groupBy(_._1).map(_._2).partition(_.size == 2) match {
      case (oldData, newData) =>
        (oldData.map(_.toSet).filter(_.size == 2).map(_.max) ++ newData.flatten).toMap
    }
    /*filter(_.size == 2).map(_.max).toSeq.sortBy { case (_, likeData) => likeData.seq }*/
    /*  finalData.groupBy(_._1).map {
        case (which, values) => values.toSet
      }.partition(_.size == 2) match {
        case (newData, oldData) =>
          (newData.map(_.max).toMap, oldData.flatten.toMap)
      }*/

    //      filter(_.size == 2).map(_.max)
  }

  def Time[A](fn: => A) {
    val time = System.currentTimeMillis()
    println(fn)
    println(System.currentTimeMillis() - time)
  }

  Time(mergeLikeData(clientData, serverData))
//  Time(mergeLikeData2(clientData, serverData))

//  Time(List(1, 2, 3, 4, 5).partition(_ % 2 == 0))

  //  def fork[A](a: => Par[A]): Par[A] =
  //    es => es.submit(new Callable[A] {
  //      def call = a(es).get
  //    })
}